package com.ijiuchuang.jc_localization.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.PopupMenu;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.TintTypedArray;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import com.ijiuchuang.jc_localization.R;
import com.ijiuchuang.jc_localization.base.BaseActivity;
import com.ijiuchuang.jc_localization.bean.entity.BleDevice;
import com.ijiuchuang.jc_localization.contract.tools.ToolsRssiContract;
import com.ijiuchuang.jc_localization.presenter.tools.ToolsRssiPresenter;
import com.ijiuchuang.jc_localization.tools.adapter.recyclerview.OnItemClickListener;
import com.ijiuchuang.jc_localization.view.adapter.ToolsRssiRecyclerAdapter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ToolsRssiActivity extends BaseActivity implements ToolsRssiContract.View {

    @BindView(R.id.tools_rssi_toolbar)
    Toolbar toolbar;
    @BindView(R.id.tools_rssi_fab)
    FloatingActionButton fab;
    @BindView(R.id.tools_rssi_coordinator)
    CoordinatorLayout mCoordinator;
    @BindView(R.id.tools_rssi_recycler_view)
    RecyclerView mRecyclerView;

    private ToolsRssiContract.Presenter presenter;
    private ToolsRssiRecyclerAdapter mRecyclerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tools_rssi);
        ButterKnife.bind(this);

        setSupportActionBar(toolbar);
        toolbar.setNavigationIcon(R.drawable.ic_arrow_back_white_24dp);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        presenter = new ToolsRssiPresenter(this, this);
    }

    @Override
    protected void onStart() {
        super.onStart();

        presenter.start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    @OnClick(R.id.tools_rssi_fab)
    public void onClick() {
        Snackbar.make(mCoordinator, R.string.snack_title_rssi_start, Snackbar.LENGTH_SHORT)
                .setAction(R.string.snack_action_ok, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        presenter.startScan();
                    }
                }).show();
    }

    @Override
    public void setPresenter(ToolsRssiContract.Presenter presenter) {
        this.presenter = presenter;
    }

    @Override
    public void openBle() {
        Snackbar.make(mCoordinator, R.string.snack_title_open_ble, Snackbar.LENGTH_INDEFINITE)
                .setAction(R.string.snack_action_ok, new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        startActivity(new Intent(Settings.ACTION_BLUETOOTH_SETTINGS)); // 直接进入手机中的蓝牙设置界面
                    }
                })
                .setCallback(new Snackbar.Callback() {
                    @Override
                    public void onDismissed(Snackbar snackbar, int event) {
                        super.onDismissed(snackbar, event);

                        if (event == Snackbar.Callback.DISMISS_EVENT_SWIPE) {
                            finish();
                        }

                        if (!fab.isShown()) {
                            fab.show();
                        }
                    }

                    @Override
                    public void onShown(Snackbar snackbar) {
                        super.onShown(snackbar);

                        if (fab.isShown()) {
                            fab.hide();
                        }
                    }
                })
                .show();
    }

    @Override
    public void setFabIcon(int iconID) {
        fab.setImageResource(iconID);
    }

    @Override
    public void setDatas(List<BleDevice> datas) {
        mRecyclerAdapter = new ToolsRssiRecyclerAdapter(this, R.layout.recycler_item_tools_rssi, datas);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mRecyclerView.setAdapter(mRecyclerAdapter);
    }

    @Override
    public void addDevice(BleDevice device) {
        mRecyclerAdapter.insertLast(device);
    }

    @Override
    public void updateDevice(int position, BleDevice device) {
        mRecyclerAdapter.updateData(position, device);
    }
}
